<?php

/**
 * Description of equipa
 *
 * @author Leonardo Lino
 */
class Equipa extends CI_Controller {

    private $smarty;

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $session_id = $this->session->userdata('session_id');
        require_once 'smartyLoader.php';
        $this->smarty = $smarty;
        $this->user->isAccessBackOffice($this->smarty);
        $this->load->model('equipaModel');
    }

    public function index() {
        $equipas = $this->equipaModel->loadAll();
        if ($equipas == null) {
            $this->smarty->assign("vazio", 0);
        } else {
            $this->smarty->assign("vazio", 1);
        }


        $this->smarty->assign("equipas", $equipas);
        $this->smarty->assign("templateActivo", "equipa.tpl.html");
        $this->smarty->display('admin/header.tpl.html');
        $this->smarty->display('admin/main.tpl.html');
        $this->smarty->display('admin/footer.tpl.html');
    }

    public function novo() {
        $state = $_POST['state'];
        if ($state == "1") {
            $resultado = $this->equipaModel->novo($_POST['equipa'], $_POST['password'], $_POST['email'], $_POST['escola'], $_POST['morada'], $_POST['localidade'], $_POST['codpostal'], $_POST['professor'], $_POST['telefone'], $_POST['elementos'], $_POST['avatar_url']);
            $this->index();
        } else {

            $resultado = $this->equipaModel->update($_POST['equipa2'], $_POST['email2'], $_POST['escola2'], $_POST['morada2'], $_POST['localidade2'],
                    $_POST['codpostal2'], $_POST['professor2'], $_POST['telefone2'], $_POST['elementos2'], $_POST['vid2']);
            $this->index();
        }
    }

    public function loadAjax() {
        $row = $this->equipaModel->loadFromId($_POST['vid']);
        echo json_encode($row);
    }
    
     public function delete (){
      $resultado = $this->equipaModel->deleteFromId($_GET['vid']);
      $this->index();
    }

}

?>
